#[cfg(test)]
mod encoding_tests {
    use crate::{EncodingDetector, HtmlParser, Internationalization, ParserConfig};

    #[test]
    fn test_encoding_detection() {
        // 测试meta charset检测
        let html = r#"
        <!DOCTYPE html>
        <html>
        <head>
            <meta charset="UTF-8">
            <title>编码测试</title>
        </head>
        <body>
            <p>中文内容测试</p>
        </body>
        </html>
        "#;

        let mut detector = EncodingDetector::new();
        let encoding = detector.detect_from_meta(html);

        assert_eq!(encoding.name(), "UTF-8");
        assert!(detector.is_detected_from_meta());

        // 测试http-equiv检测
        let html2 = r#"
        <!DOCTYPE html>
        <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>编码测试</title>
        </head>
        <body>
            <p>中文内容测试</p>
        </body>
        </html>
        "#;

        let mut detector = EncodingDetector::new();
        let encoding = detector.detect_from_meta(html2);

        assert_eq!(encoding.name(), "UTF-8");
        assert!(detector.is_detected_from_meta());

        // 测试XML声明
        let html3 = r#"<?xml version="1.0" encoding="UTF-8"?>
        <!DOCTYPE html>
        <html>
        <head>
            <title>编码测试</title>
        </head>
        <body>
            <p>中文内容测试</p>
        </body>
        </html>
        "#;

        let mut detector = EncodingDetector::new();
        let encoding = detector.detect_from_meta(html3);

        assert_eq!(encoding.name(), "UTF-8");
        assert!(detector.is_detected_from_meta());
    }

    #[test]
    fn test_entity_decoding() {
        let i18n = Internationalization::new();

        // 测试命名实体
        let input = "&lt;div&gt;&amp;nbsp;&quot;test&quot;&apos;";
        let expected = "<div>&nbsp;\"test\"'";

        assert_eq!(i18n.decode_html_entities(input), expected);

        // 测试数字实体（十进制）
        let input2 = "&#65;&#66;&#67;";
        let expected2 = "ABC";

        assert_eq!(i18n.decode_html_entities(input2), expected2);

        // 测试数字实体（十六进制）
        let input3 = "&#x41;&#X42;&#x43;";
        let expected3 = "ABC";

        assert_eq!(i18n.decode_html_entities(input3), expected3);

        // 测试混合实体
        let input4 = "&#x41;&lt;&#66;&gt;&#x43;";
        let expected4 = "A<B>C";

        assert_eq!(i18n.decode_html_entities(input4), expected4);
    }

    #[test]
    fn test_rtl_detection() {
        let i18n = Internationalization::new();

        // 测试RTL语言
        assert!(i18n.is_rtl_language("ar"));
        assert!(i18n.is_rtl_language("he"));
        assert!(i18n.is_rtl_language("ar-EG"));

        // 测试LTR语言
        assert!(!i18n.is_rtl_language("en"));
        assert!(!i18n.is_rtl_language("zh-CN"));
        assert!(!i18n.is_rtl_language("fr-FR"));
    }

    #[test]
    fn test_integration() {
        let config = ParserConfig {
            enable_i18n: true,
            decode_entities: true,
            ..Default::default()
        };

        let parser = HtmlParser::new(config);

        // 测试带有国际化元素的HTML
        let html = r#"
        <!DOCTYPE html>
        <html>
        <head>
            <meta charset="UTF-8">
            <title>国际化测试</title>
        </head>
        <body>
            <p lang="en">English text</p>
            <p lang="zh-CN">中文文本</p>
            <p lang="ar" dir="rtl">النص العربي</p>
            <p>实体测试: &copy; &#169; &#x00A9;</p>
        </body>
        </html>
        "#;

        let dom = parser.parse(html).unwrap();

        // 测试语言检测
        let p_elements = parser.query_selector(&dom, "p").unwrap();

        assert_eq!(p_elements[0].get_language(), Some("en"));
        assert_eq!(p_elements[1].get_language(), Some("zh-CN"));
        assert_eq!(p_elements[2].get_language(), Some("ar"));

        // 测试方向检测
        assert!(!p_elements[0].is_rtl());
        assert!(!p_elements[1].is_rtl());
        assert!(p_elements[2].is_rtl());

        // 测试编码检测
        assert_eq!(parser.get_detected_encoding(), "UTF-8");

        // 测试实体解码
        assert_eq!(p_elements[3].text_content(), "实体测试: © © ©");
    }
}
